import { ConfigData } from "./ConfigData";
import { LogUtil } from "./LogUtil";

export const TraceEntry =
  (target: Object, methodName: string, propertyDescriptor: PropertyDescriptor): PropertyDescriptor => {
    const method = propertyDescriptor.value;

    propertyDescriptor.value = function (...args: any[]) {
      const params = args.map(a => JSON.stringify(a)).join();
      LogUtil.info(ConfigData.TAG + `${target.constructor.name}#${methodName}(${params}) in from ${new Error().stack}`);

      console.time(`${target.constructor.name}#${methodName}(${params})`);
      const result = method.apply(this, args);
      console.timeEnd(`${target.constructor.name}#${methodName}(${params})`);
      const r = JSON.stringify(result);

      LogUtil.info(ConfigData.TAG + `${target.constructor.name}#${methodName}(${params}) out => ${r}`);
      return result;
    }

    return propertyDescriptor;
  };